package edu.ucla.wanda.social;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.joda.time.DateTime;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class FacebookUserAdapter {
	String userId, authToken, tokenExpires;

	private JsonObject me;

	private static final String APP_ID = "321155711249351";

	public FacebookUserAdapter(String userId, String authToken,
			String tokenExpires) throws IOException {
		this.userId = userId;
		this.authToken = authToken;
		this.tokenExpires = tokenExpires;

		if (isExpired()) {
			refreshToken();
		}

		getMeInfo();
	}

	public boolean isExpired() {
		Long expireTime = Long.parseLong(tokenExpires);

		if (expireTime > 0) {
			if (DateTime.now().isAfter(expireTime)) {
				return true;
			}
		}

		return false;
	}

	public void getMeInfo() throws IOException {
		// retrieve me info
		if (null != authToken) {
			URL url = new URL("https://graph.facebook.com/me?access_token="
					+ authToken);
			HttpURLConnection cxn = (HttpURLConnection) url.openConnection();

			cxn.setRequestMethod("GET");
			cxn.setRequestProperty("Accept", "application/json");

			me = new JsonParser().parse(
					new InputStreamReader(cxn.getInputStream()))
					.getAsJsonObject();
			cxn.disconnect();
		}
	}
	
	/**
	 * @return the userId
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * @param userId the userId to set
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * @return the authToken
	 */
	public String getAuthToken() {
		return authToken;
	}

	/**
	 * @param authToken the authToken to set
	 */
	public void setAuthToken(String authToken) {
		this.authToken = authToken;
	}

	/**
	 * @return the tokenExpires
	 */
	public String getTokenExpires() {
		return tokenExpires;
	}

	/**
	 * @param tokenExpires the tokenExpires to set
	 */
	public void setTokenExpires(String tokenExpires) {
		this.tokenExpires = tokenExpires;
	}

	public String getMeField(String fieldName) throws IOException
	{
		if(me.get(fieldName).isJsonNull())
		{
			return null;
		}
		
		return me.get(fieldName).getAsString();
	}

	public void refreshToken() {
		// todo
	}

	public void postToWall(String message) throws IOException {
		URL url = new URL("https://graph.facebook.com/" + userId + "/feed");
		HttpURLConnection cxn = (HttpURLConnection) url.openConnection();

		cxn.setRequestMethod("POST");
		cxn.addRequestProperty("access_token", authToken);
		cxn.addRequestProperty("message", message);
		cxn.addRequestProperty("type", "status");
		cxn.addRequestProperty("link", "https://apps.facebook.com/wanda_social");
		cxn.addRequestProperty("name", "WANDA Social");
		cxn.addRequestProperty("caption", "test caption");

		cxn.setRequestProperty("Accept", "application/json");

		JsonObject o = new JsonParser().parse(
				new InputStreamReader(cxn.getInputStream())).getAsJsonObject();

		if (o.has("error")) {
			throw new IOException(o.toString());
		}
	}

}
